package com.ohxwj.common.utils;

import org.icepdf.core.exceptions.PDFException;
import org.icepdf.core.exceptions.PDFSecurityException;
import org.icepdf.core.pobjects.Document;
import org.icepdf.core.util.GraphicsRenderingHints;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

import static java.lang.System.currentTimeMillis;

/**

 * Created by Administrator on 2017/10/19.
 */
public class Pdf2PicUtils {

    public static void pdf2Pic(String pdfPath, String picPath, String htmlPath) throws IOException, PDFException, PDFSecurityException, InterruptedException {


        Document document = new Document();
        document.setFile(pdfPath);
        float scale = 2.5f;// 缩放比例
        float rotation = 0f;// 旋转角度
        String html="";

        for (int i = 0; i < document.getNumberOfPages(); i++) {
            BufferedImage image = (BufferedImage) document.getPageImage(i, GraphicsRenderingHints.SCREEN,
                    org.icepdf.core.pobjects.Page.BOUNDARY_CROPBOX, rotation, scale);
            RenderedImage rendImage = image;
            try {
                String imgName = currentTimeMillis() + ".png";
                File file = new File(picPath + imgName);
                ImageIO.write(rendImage, "png", file);
                html+="<img style='width:100%' src='/upload/notices/school/images/"+imgName+"'>";
            } catch (IOException e) {
                e.printStackTrace();
            }
            image.flush();
        }

        //用于存储html字符串
        StringBuilder stringHtml = new StringBuilder();

        //打开文件
        PrintStream printStream = new PrintStream(new FileOutputStream(htmlPath));

        //输入HTML文件内容
        stringHtml.append("<html><head>");
        stringHtml.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");
        stringHtml.append("<title></title>");
        stringHtml.append("</head>");
        stringHtml.append("<body>");
        stringHtml.append(html);
        stringHtml.append("</body></html>");
        try{
            //将HTML文件内容写入文件中

            printStream.println(stringHtml.toString());
        }catch (Exception e) {

            e.printStackTrace();
        }


        document.dispose();
    }


    public static List<String> pdf2PicImgList(String pdfPath, String picPath) throws IOException, PDFException, PDFSecurityException, InterruptedException {


        Document document = new Document();
        document.setFile(pdfPath);
        float scale = 2.5f;// 缩放比例
        float rotation = 0f;// 旋转角度
        String html="";
        List<String> list=new ArrayList<>();

        for (int i = 0; i < document.getNumberOfPages(); i++) {
            BufferedImage image = (BufferedImage) document.getPageImage(i, GraphicsRenderingHints.SCREEN,
                    org.icepdf.core.pobjects.Page.BOUNDARY_CROPBOX, rotation, scale);
            RenderedImage rendImage = image;
            try {
                String imgName = currentTimeMillis() + ".png";
                File file = new File(picPath + imgName);
                ImageIO.write(rendImage, "png", file);

                list.add("/upload/notices/school/images/"+imgName+"");

            } catch (IOException e) {
                e.printStackTrace();
            }
            image.flush();
        }

        document.dispose();

        return list;
    }


    public static void main(String args[]) throws InterruptedException, PDFSecurityException, PDFException, IOException {
        pdf2Pic("D:/upload/notices/school/word/666.pdf","D:/upload/notices/school/word/","D:/upload/notices/school/word/");

        //用于存储html字符串
        StringBuilder stringHtml = new StringBuilder();

        //打开文件
        PrintStream printStream = new PrintStream(new FileOutputStream("D:/upload/notices/school/word/test2.html"));

        //输入HTML文件内容
        stringHtml.append("<html><head>");
        stringHtml.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");
        stringHtml.append("<title>测试报告文档</title>");
        stringHtml.append("</head>");
        stringHtml.append("<body>");
        stringHtml.append("<div>hello</div>");
        stringHtml.append("</body></html>");
        try{
            //将HTML文件内容写入文件中

            printStream.println(stringHtml.toString());
        }catch (Exception e) {

            e.printStackTrace();
        }

    }


}
